<template>
  <section>
    <el-col v-show="pwdVisible">
      <el-form label-width="6rem" ref="newForm" class="pwdForm">
        <el-form-item label="密码">
          <el-input
            v-model="initializePwd"
            type="password"
            placeholder="请输入初始化密码"
            style="width:15rem"
          ></el-input>
        </el-form-item>
      </el-form>
      <h2 class="pwdTitle">注：初始化房间信息前，请先备份数据库，以免想恢复到以前状态时数据丢失</h2>
      <el-button type="primary" class="pwdButton" @click="confirmPwd">确认</el-button>
    </el-col>

    <el-form
      label-width="8rem"
      ref="initializeForm"
      class="initializeForm"
      v-show="formVisible"
      :model="initializeData"
      :rules="rules"
      v-loading="loading"
    >
      <el-form-item label="所属区域" prop="AR_number" required>
        <el-select v-model="initializeData.AR_number" placeholder="区域编号" @change="areaChange">
          <el-option v-for="item in area" :key="item.value" :label="item.label" :value="item.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="宿舍楼编号" prop="SSL_number" required>
        <el-select
          v-model="initializeData.SSL_number"
          placeholder="宿舍楼编号"
          @change="dormChange(initializeData.SSL_number)"
          clearable
        >
          <el-option v-for="item in dorm" :key="item.value" :label="item.label" :value="item.value"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="初始化楼层" required>
        <el-col :span="11">
          <el-form-item prop="SS_floor1">
            <el-select v-model="initializeData.SS_floor1" placeholder="楼层">
              <el-option
                v-for="item in layers"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col class="line" :span="1" :offset="6.5">-</el-col>
        <el-col :span="11">
          <el-form-item prop="SS_floor2">
            <el-select v-model="initializeData.SS_floor2" placeholder="楼层">
              <el-option
                v-for="item in layers"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-form-item>
      <el-form-item label="房间数量" prop="SS_room_number" required>
        <el-input v-model.number="initializeData.SS_room_number"></el-input>
      </el-form-item>
      <el-form-item label="房间类型" prop="SS_roomtype" required>
        <el-radio-group v-model="initializeData.SS_roomtype">
          <el-radio label="0">单人间</el-radio>
          <el-radio label="1">多人间</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="入住性别" prop="SS_dormsex" required>
        <el-radio-group v-model="initializeData.SS_dormsex">
          a
          <el-radio label="1">男</el-radio>
          <el-radio label="0">女</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="房间编号前缀" prop="SS_number_Prefix">
        <el-input v-model="initializeData.SS_number_Prefix"></el-input>
      </el-form-item>
      <el-form-item label="可住人数" prop="SS_bednumber" required>
        <el-select v-model="initializeData.SS_bednumber" placeholder="人数">
          <el-option
            v-for="item in personNumber"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="柜子数量" prop="SS_cabinet">
        <el-select v-model="initializeData.SS_cabinet" placeholder="柜子数量">
          <el-option
            v-for="item in SS_cabinet"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="住宿费用" prop="SS_cost">
        <el-input v-model.number="initializeData.SS_cost"></el-input>
      </el-form-item>
      <el-form-item label="费用承担" prop="SS_costman" required>
        <el-radio-group v-model="initializeData.SS_costman">
          <el-radio label="0">公司承担</el-radio>
          <el-radio label="1">员工承担</el-radio>
          <el-radio label="2">公摊</el-radio>
          <el-radio label="3">现结类冷水(洗衣机类型)</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="默认房间状态" prop="SS_status" required>
        <el-radio-group v-model="initializeData.SS_status">
          <el-radio label="0">启用</el-radio>
          <el-radio label="1">禁用</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="是否临时入住" prop="SS_temporary" required>
        <el-radio-group v-model="initializeData.SS_temporary">
          <el-radio label="0">是</el-radio>
          <el-radio label="1">否</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item>
        <el-button @click.native="resettingSubmit">重置</el-button>
        <el-button type="primary" @click.native="initializeSubmit('initializeForm')">确认</el-button>
      </el-form-item>
    </el-form>
  </section>
</template>
<script>
import { area_select } from "../../api/area.js";
import {
  room_form,
  room_initialization,
} from "../../api/RoomInitialization.js";
import { building_select } from "../../api/floor.js";
export default {
  data() {
    return {
      initializePwd: null,
      pwdVisible: true,
      formVisible: false,
      initializeData: {
        AR_number: null,
        SSL_number: null,
        SS_floor1: null,
        SS_floor2: null,
        SS_room_number: null,
        SS_roomtype: null,
        SS_dormsex: null,
        SS_number_Prefix: null,
        SS_bednumber: null,
        SS_cost: null,
        SS_status: null,
        SS_temporary: null,
        SS_cabinet: 0,
        SS_costman: null,
      },
      area: [
        // {
        //   value: "",
        //   label: ""
        // }
      ],
      dorm: [
        // {
        //   value: "",
        //   label: ""
        // }
      ],
      layers: [],
      personNumber: [
        {
          value: "1",
          label: "1",
        },
        {
          value: "2",
          label: "2",
        },
        {
          value: "3",
          label: "4",
        },
        {
          value: "4",
          label: "4",
        },
        {
          value: "5",
          label: "5",
        },
        {
          value: "6",
          label: "6",
        },
      ],
      SS_cabinet: [
        {
          value: "0",
          label: "0",
        },
        {
          value: "1",
          label: "1",
        },
        {
          value: "2",
          label: "2",
        },
        {
          value: "3",
          label: "3",
        },
        {
          value: "4",
          label: "4",
        },
        {
          value: "5",
          label: "5",
        },
        {
          value: "6",
          label: "6",
        },
        {
          value: "7",
          label: "7",
        },
        {
          value: "8",
          label: "8",
        },
      ],
      rules: {
        AR_number: [
          { required: true, message: "请选择区域", trigger: "change" },
        ],
        SSL_number: [
          { required: true, message: "请选择宿舍楼", trigger: "change" },
        ],
        SS_floor1: [
          { required: true, message: "请选择起始楼层", trigger: "change" },
        ],
        SS_floor2: [
          { required: true, message: "请选择结束楼层", trigger: "change" },
        ],
        SS_room_number: [
          { required: true, message: "请输入房间数量", trigger: "blur" },
        ],
        SS_roomtype: [
          { required: true, message: "请选择房间类型", trigger: "change" },
        ],
        SS_dormsex: [
          { required: true, message: "请选择入住性别", trigger: "change" },
        ],
        SS_bednumber: [
          { required: true, message: "请选择可入住人数", trigger: "change" },
        ],
        SS_cost: [
          { required: false, message: "请输入房间费用", trigger: "blur" },
        ],
        SS_status: [
          { required: true, message: "请选择房间状态", trigger: "change" },
        ],
        SS_temporary: [
          { required: true, message: "请选择是否临时入住", trigger: "change" },
        ],
        SS_costman: [
          { required: true, message: "请选择费用承担方", trigger: "change" },
        ],
      },
      loading: false,
    };
  },
  methods: {
    // 密码确认
    confirmPwd() {
      room_initialization(this.initializePwd).then((res) => {
        if (res) {
          this.pwdVisible = false;
          this.formVisible = true;
        }
      });
    },
    getArea() {
      area_select({ AR_number: "", AR_name: "", page: 1 }).then((res) => {
        if (res) {
          // this.initializeData.AR_number = res.data;
          // console.log(res.data.length);
          for (let index = 0; index < res.data.length; index++) {
            this.area.push({
              label: res.data[index].AR_name,
              value: res.data[index].AR_number,
            });
          }
        }
      });
    },
    //获取区域对应宿舍楼
    areaChange() {
      if (this.initializeData.AR_number === "") {
        return true;
      }
      console.log(this.initializeData.AR_number);
      this.dorm = [];
      building_select({
        AR_number: this.initializeData.AR_number,
        SSL_number: "",
        SSL_admin: "",
        page: 1,
      }).then((res) => {
        if (res) {
          for (let index = 0; index < res.data.length; index++) {
            this.dorm.push({
              label: res.data[index].SSL_name,
              value: res.data[index].SSL_number,
            });
          }
        } else {
          this.dorm = [
            {
              value: "",
              label: "",
            },
          ];
          this.initializeData.SSL_number = "";
        }
      });
    },
    //获取宿舍楼对应最多楼层
    dormChange(dorm) {
      if (dorm === "") {
        return true;
      }
      // 获取当前楼层
      building_select({
        AR_number: this.initializeData.AR_number,
        SSL_number: this.initializeData.SSL_number,
        SSL_admin: "",
        page: 1,
      }).then((res) => {
        if (res) {
          let maxfloor = res.data[0].SSL_floornumber;
          // console.log(maxfloor);
          this.layers = [];
          for (let i = 1; i <= maxfloor; i++) {
            let floor = { value: i, label: i };
            this.layers.push(floor);
          }
        }
      });
    },
    initializeSubmit(form) {
      this.$refs[form].validate((valid) => {
        if (valid) {
          this.loading = true;
          // console.log(this.initializeData);
          room_form(this.initializeData).then((res) => {
            this.loading = false;
            if (res) {
              this.$alert(res.msg, "操作提示", {
                confirmButtonTextL: "确定",
                callback: (action) => {
                  this.alterFormVisible = false;
                },
              });
            }
          });
        } else {
          this.$message.error("信息未填完整");
        }
      });
    },
    // 重置
    resettingSubmit() {
      this.initializeData = {
        AR_number: null,
        SSL_number: null,
        SS_floor1: null,
        SS_floor2: null,
        SS_room_number: null,
        SS_roomtype: null,
        SS_dormsex: null,
        SS_number_Prefix: null,
        SS_bednumber: null,
        SS_cost: null,
        SS_status: null,
        SS_temporary: null,
      };
    },
  },
  mounted() {
    this.getArea();
  },
};
</script>
<style>
.pwdForm {
  /* 实现元素居中 */
  position: absolute;
  top: 20%; /* 上浮一点 */
  left: 50%;
  transform: translate(-50%, -50%);
  /* align-self: center  此属性目前暂未支持，一键垂直居中*/
}
.pwdTitle {
  position: absolute;
  top: 30%; /* 上浮一点 */
  left: 50%;
  transform: translate(-50%, -50%);
  color: rgb(243, 51, 51);
  font-size: 1.5rem;
}
.pwdButton {
  position: absolute;
  top: 50%; /* 上浮一点 */
  left: 50%;
  transform: translate(-50%, -50%);
  width: 10rem;
}
.initializeForm {
  width: 45em;
  /* height: 55em; */
  padding: 1.5em 1.5em;
  border: 1px solid rgb(178, 194, 184);
  border-radius: 2%;
  /* position: absolute; */
  /* top: 50%; 上浮一点 */
  /* left: 50%; */
  /* transform: translate(-50%, -50%); */
  margin: auto;
}
</style>